package com.ktg.mes.md.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktg.common.core.domain.APIResult;
import com.ktg.common.utils.StringUtils;
import com.ktg.mes.md.domain.MdItem;
import com.ktg.mes.md.domain.MdStatisticsTypeBom;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.core.domain.Result;
import com.ktg.mes.md.domain.MdProductStatisticsBom;
import com.ktg.mes.md.service.IMdProductStatisticsBomService;
import com.ktg.common.utils.poi.ExcelUtil;

/**
 * 产品的统计分类BOMController
 *
 * @author byteco
 * @date 2023-02-22
 */
@RestController
@RequestMapping("/md/mdProductStatisticsBom")
public class MdProductStatisticsBomController extends BaseController
{
    @Autowired
    private IMdProductStatisticsBomService mdProductStatisticsBomService;

    /**
     * 查询产品的统计分类BOM列表
     */
    @PreAuthorize("@ss.hasPermi('md:mdProductStatisticsBom:list')")
    @GetMapping("/list")
    public Result<IPage<MdProductStatisticsBom>> list(MdProductStatisticsBom mdProductStatisticsBom,
                                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                            HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("查询产品的统计分类BOM列表",req,null);
        try {
            Page<MdProductStatisticsBom> page = new Page<MdProductStatisticsBom>(pageNo, pageSize);
            IPage<MdProductStatisticsBom> pageList = mdProductStatisticsBomService.queryPageList(mdProductStatisticsBom,page);
            return apiLog.ok(pageList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 根据产品编号，查询统计分类的统计分类BOM列表
     */
    @GetMapping("/bomlistFromItemId")
    public Result bomlistFromItemId(@RequestParam(name="itemId") Long itemId,
                          HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("查询统计分类的统计分类BOM列表",req,null);
        try {
            if (itemId == null && itemId < 1){
                return apiLog.ok("产品编号 不能为空");
            }

            MdProductStatisticsBom bom = new MdProductStatisticsBom();
            bom.setItemId(itemId);

            QueryWrapper<MdProductStatisticsBom> typeBomQueryWrapper = new QueryWrapper<>(bom);
            typeBomQueryWrapper.orderByDesc("quantity");
            List<MdProductStatisticsBom> pageList = mdProductStatisticsBomService.list(typeBomQueryWrapper);
            return apiLog.ok(pageList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }


    /**
     * 导出产品的统计分类BOM列表
     */
    @PreAuthorize("@ss.hasPermi('md:mdProductStatisticsBom:export')")
    @Log(title = "产品的统计分类BOM", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdProductStatisticsBom mdProductStatisticsBom,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("导出产品的统计分类BOM列表",req,mdProductStatisticsBom);
        try {
            List<MdProductStatisticsBom> list = mdProductStatisticsBomService.queryList(mdProductStatisticsBom);
            ExcelUtil<MdProductStatisticsBom> util = new ExcelUtil<MdProductStatisticsBom>(MdProductStatisticsBom.class);
            util.exportExcel(response, list, "产品的统计分类BOM数据");
            apiLog.ok(null);
        }catch (Exception e){
            apiLog.error(e);
        }
    }

    /**
     * 获取产品的统计分类BOM详细信息
     */
    @PreAuthorize("@ss.hasPermi('md:mdProductStatisticsBom:query')")
    @GetMapping(value = "/{bomId}")
    public Result<MdProductStatisticsBom> getInfo(@PathVariable("bomId") Long bomId,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("获取产品的统计分类BOM详细信息",req,null);
        try {
            return apiLog.ok(mdProductStatisticsBomService.getInfo(bomId));
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 新增产品的统计分类BOM
     */
    @PreAuthorize("@ss.hasPermi('md:mdProductStatisticsBom:add')")
    @Log(title = "产品的统计分类BOM", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<String> add(@RequestBody MdProductStatisticsBom mdProductStatisticsBom,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("新增产品的统计分类BOM",req,mdProductStatisticsBom);
        try {
            MdProductStatisticsBom ff = new MdProductStatisticsBom();
            ff.setStatisticsId(mdProductStatisticsBom.getStatisticsId());
            ff.setItemId(mdProductStatisticsBom.getItemId());
            QueryWrapper<MdProductStatisticsBom> queryWrapper = new QueryWrapper<MdProductStatisticsBom>(ff);
            long a = mdProductStatisticsBomService.count(queryWrapper);
            if (a > 0){
                return apiLog.fail("数据已存在");
            }
            mdProductStatisticsBomService.insert(mdProductStatisticsBom);
            return apiLog.ok("添加成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 修改产品的统计分类BOM
     */
    @PreAuthorize("@ss.hasPermi('md:mdProductStatisticsBom:edit')")
    @Log(title = "产品的统计分类BOM", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public Result<String> edit(@RequestBody MdProductStatisticsBom mdProductStatisticsBom,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("修改产品的统计分类BOM",req,mdProductStatisticsBom);
        try {
            mdProductStatisticsBomService.update(mdProductStatisticsBom);
            return apiLog.ok("编辑成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 删除产品的统计分类BOM
     */
    @PreAuthorize("@ss.hasPermi('md:mdProductStatisticsBom:remove')")
    @Log(title = "产品的统计分类BOM", businessType = BusinessType.DELETE)
	@DeleteMapping("/{bomIds}")
    public Result<String> remove(@PathVariable Long[] bomIds,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("删除产品的统计分类BOM",req,null);
        try {
            mdProductStatisticsBomService.delete(bomIds);
            return apiLog.ok("删除成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }

    }
}
